package com.qk.controller;

import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.entity.PageResult;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/courses")
@Slf4j
public class CourseController {
    @Autowired private CourseService courseService;
    @GetMapping
    public Result list(CourseDto courseDto) {
        PageResult<Course> pageResult = courseService.list(courseDto);
        return Result.success(pageResult);
    }
    @DeleteMapping("/{id}")
    public Result delete( @PathVariable Integer id) {
        courseService.delete(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Course course) {
        courseService.add(course);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getByID(@PathVariable Integer id) {
        Course course = courseService.getByID(id);
        return Result.success(course);
    }
    @PutMapping
    public Result update(@RequestBody Course course) {
        courseService.update(course);
        return Result.success();
    }
    @GetMapping("/list")
    public Result getAll() {
        return Result.success(courseService.getAll());
    }
    @GetMapping("/subject/{subject}")
    public Result getBySubject(@PathVariable Integer subject) {
        return Result.success(courseService.getBySubject(subject));
    }
}
